// 这个文件是 "main.dart"
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:flutter/foundation.dart';
part 'setting.freezed.dart';
part 'setting.g.dart';

@freezed
class SleepSetting with _$SleepSetting {
  const factory SleepSetting({
    double? multiple,
    double? deep,
    double? shallow,
  }) = _PluseSettings;

  factory SleepSetting.fromJson(Map<String, Object?> json) =>
      _$SleepSettingFromJson(json);

  static defaultSetting() {
    return const SleepSetting(multiple: 1, deep: 15.9, shallow: 14);
  }

  static Map title() {
    return {
      "multiple": "Multiple",
      "deep": "RR: awake-light sleep",
      "shallow": "RR: light sleep-deep sleep",
    };
  }
}

extension SCombine on SleepSetting {
  conbine(SleepSetting def) {
    return SleepSetting.fromJson(
        toJson().combine(SleepSetting.defaultSetting().toJson()));
  }
}

@freezed
class PluseSetting with _$PluseSetting {
  const factory PluseSetting({
    double? Multiple,
  }) = _PluseSetting;

  factory PluseSetting.fromJson(Map<String, Object?> json) =>
      _$PluseSettingFromJson(json);

  static defaultSetting() {
    return const PluseSetting(Multiple: 1);
  }
}

@freezed
class FatigueSetting with _$FatigueSetting {
  const factory FatigueSetting({
    double? multiple,
    double? CTsevere,
    double? CTmoderate,
    double? CTmild,
    double? PERCLOSsevere,
    double? PERCLOSmoderate,
    double? PERCLOSmild,
    double? HRVsevere,
    double? HRVmoderate,
    double? HRVmild,
  }) = _FatigueSetting;

  factory FatigueSetting.fromJson(Map<String, Object?> json) =>
      _$FatigueSettingFromJson(json);

  static defaultSetting() {
    return const FatigueSetting(
      multiple: 1.0,
      CTsevere: 3.0,
      CTmoderate: 1.0,
      CTmild: 0.4,
      PERCLOSsevere: 40,
      PERCLOSmoderate: 20,
      PERCLOSmild: 8.5,
      HRVsevere: 0.04, //<
      HRVmoderate: 0.075, //<
      HRVmild: 0.1, //
    );
  }

  static Map title() {
    return {
      "multiple": "Multiple",
      "HRVmild": "HRV(s): normal-mild",
      "HRVmoderate": "HRV(s): mild-moderate",
      "HRVsevere": "HRV(s): moderate-severe",
      "CTmild": "CT(s): normal-mild",
      "CTmoderate": "CT(s): mild-moderate",
      "CTsevere": "CT(s): moderate-severe",
      "PERCLOSmild": "PERCLOS(%): normal-mild",
      "PERCLOSmoderate": "PERCLOS(%): mild-moderate",
      "PERCLOSsevere": "PERCLOS(%): moderate-severe",
    };
  }
}

extension Combine on FatigueSetting {
  conbine(FatigueSetting def) {
    return FatigueSetting.fromJson(
        toJson().combine(FatigueSetting.defaultSetting().toJson()));
  }
}

extension Conbine on Map {
  Map<String, dynamic> combine(Map other) {
    Map<String, dynamic> comMap = {};
    for (final key in keys) {
      final value = this[key] ?? other[key];
      comMap[key] = value;
    }

    for (final key in other.keys) {
      if (!containsKey(key)) {
        comMap[key] = other[key];
      }
    }

    return comMap;
  }
}
